﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using DG.Tweening;
using tools;

public class ZjhPlayer : MonoBehaviour {
	
	public long Id = 0;
	public int PlayerIndex = -1;
	public Image Head;
	private Text NameTxt;
	private Text MoneyTxt;
	public string P_name = "";
	public string P_money = "";
	public bool None = false;
	private Image ready = null;
	public List<ZJHCards> cardList = new List<ZJHCards> ();
	public List<ZJHCards> tempList = new List<ZJHCards> ();
	public GameObject CardPanel;
	public GameObject ComparePanel;
	public bool master = false;
	private Image masterImg = null;
	private WWW HeadLoader = null;
	public bool GameIng = false;
	public bool Offline = false;
	private Image offlineImg = null;
	private Image Halo = null;
	private bool HaloColor = false;
	private Image Winer = null;
	private Image Face = null;
	private Image voiceimg = null;
	public Sprite voice = null;
	public Image Spreak = null;
	public int SpreakTime = 240;
	public Image Woner = null;
	public bool Open = true;
	public int childIndex;
	private bool initComplete = false;
	public List<int> list = new List<int>();
	private int cardIndex = 0;
	private PickCallback callback = null;
	public int CardType = 0;
	private int index = 0;
	private int y = 0;
	private int ShowIndex = 0;
	private thirteen.RoundPlayerBillInfo BillInfo;

	// Use this for initialization
	void Start () {
		
	
		Head = transform.GetChild (0).GetChild (0).GetChild (0).GetComponent<Image> ();
		NameTxt = transform.GetChild (1).GetComponent<Text> ();
		MoneyTxt = transform.GetChild (2).GetComponent<Text> ();
		CardPanel = transform.GetChild (3).GetChild(0).gameObject;
		ComparePanel = transform.GetChild (3).GetChild(1).gameObject;
		ready = transform.GetChild(4).GetComponent<Image> ();
		masterImg = transform.GetChild(5).GetComponent<Image> ();
		Halo = transform.GetChild(6).GetComponent<Image> ();
		Winer = transform.GetChild(7).GetComponent<Image> ();
		offlineImg = transform.GetChild(8).GetComponent<Image> ();
		Face = transform.GetChild(9).GetComponent<Image> ();
		voiceimg = transform.GetChild(10).GetComponent<Image> ();
		Spreak = transform.GetChild(11).GetComponent<Image> ();
	
		Spreak.gameObject.SetActive (false);
		Winer.gameObject.SetActive (false);
		EventTriggerListener.Get (transform.GetChild (0).gameObject).onClick = ShowPlayerDtial;
		Face.gameObject.SetActive (false);
		voiceimg.gameObject.SetActive (false);
		HideHalo ();

		if ( P_name.Length > 4 ) {
			 P_name = P_name.Substring (0, 4);
		}
		initComplete = true;
	}

	public void setCardList(List<int> value){

		list = value;

		List<ZJHCards> sortlist = new List<ZJHCards> ();
		for( int i = 0 ; i < list.Count ; i++ ){
			sortlist.Add(new ZJHCards ());
			sortlist [i].setValue (list [i]);
		}

		list.Clear ();
		for( int s = 0 ; s < 14 ; s++ ){
			for( int i = 0 ; i < sortlist.Count ; i++ ){
				if(sortlist[i].Value == s){
					list.Add (sortlist [i].getValue ());
				}
			}
		}

	}

	public void setCallBack(PickCallback value){
		callback = value;
	}

	public void setCardUse(){
		for( int i = 0 ; i < GroupCard.UsedCard.Count ; i++ ){
			for( int s = 0 ; s < cardList.Count ; s++ ){
				if( cardList[s].getValue() == GroupCard.UsedCard[i] ){
					cardList [s].gameObject.SetActive (false);
				}
			}
		}
	}

	public void deCardUse( List<int> l ){
		for( int i = 0 ; i < l.Count ; i++ ){
			for( int s = 0 ; s < cardList.Count ; s++ ){
				if( cardList[s].getValue() == l[i] ){
					cardList [s].gameObject.SetActive (true);
				}
			}
		}
	}

	public void setCompareCard(thirteen.RoundPlayerBillInfo value){

		BillInfo = value;

		if( cardList.Count > 0 ){
			foreach (ZJHCards z in cardList) {
				Destroy (z.gameObject);
				Destroy (z);
			}
			cardList = new List<ZJHCards> ();
		}

		BuildCompareCard ();
	}

	private void BuildCompareCard(){
		ZJHCards zjhc = null;
		for(int i = 0 ; i < 3 ; i++ ){
			zjhc = GetCloseCard (BillInfo.headCards[i]);
			zjhc.transform.localPosition = new Vector3 (-50 + i * 50, 0, 0);
			cardList.Add (zjhc);
		}

		for(int i = 0 ; i < 5 ; i++ ){
			zjhc = GetCloseCard (BillInfo.midCards[i]);
			zjhc.transform.localPosition = new Vector3 (-100 + i * 50, -80, 0);
			cardList.Add (zjhc);
		}

		for(int i = 0 ; i < 5 ; i++ ){
			zjhc = GetCloseCard (BillInfo.tailCards[i]);
			zjhc.transform.localPosition = new Vector3 (-100 + i * 50, -160, 0);
			cardList.Add (zjhc);
		}
	
	}

	private ZJHCards GetCloseCard(int value){
		GameObject card =  (GameObject)Instantiate (GameObject.Find ("CardsPre"),ComparePanel.transform);
		card.transform.SetParent(ComparePanel.transform);
		card.transform.localPosition = Vector3.zero;
		card.GetComponent<ZJHCards> ().setValue (value);
		return card.GetComponent<ZJHCards>();
	}

	public void ShowCard(){
		if(ShowIndex == 0){
			for(int i = 0 ; i < 3 ; i++ ){
				cardList [i].ChangeStyle (cardList [i].getValue ().ToString());
				cardList [i].JumpAction ();
			}
		}

		if(ShowIndex == 1){
			for(int i = 3 ; i < 8 ; i++ ){
				cardList [i].ChangeStyle (cardList [i].getValue ().ToString());
				cardList [i].JumpAction ();
			}
		}

		if(ShowIndex == 2){
			for(int i = 8 ; i < 13 ; i++ ){
				cardList [i].ChangeStyle (cardList [i].getValue ().ToString());
				cardList [i].JumpAction ();
			}
		}

		ShowIndex++;
	}

	private void ShowPlayerDtial(GameObject go){
		if( Id != 0 ){
			evt.ServerEventDispather.getInstance ().Add (evt.ServerEvent.SHOW_PLAYER_DETAIL, this);
		}
	}

	public void SetFace(string sprstr){
		Face.gameObject.SetActive (true);
		Dictionary<string,Sprite> dic = ZJHFace.dic;
		Face.sprite = (Sprite)ZJHFace.dic[sprstr];
		InvokeRepeating ("CloseFace",3f, 0f);
	}

	public void SetVoice(string url){
		voiceimg.gameObject.SetActive (true);
		voiceimg.sprite = voice;
		VoiceChatMnager.Instance.YYSDK_DownloadVoice (url,Id,SpreakIt);
		InvokeRepeating ("CloseVoice",6f, 0f);
	}

	private void SpreakIt(bool flag){
		VoiceChatMnager.Instance.YYSDK_PlayRecordVoice (Id);
	}

	public void SetSpeark(string content){
		SpreakTime = 240;
		Spreak.gameObject.SetActive (true);
		Spreak.transform.GetChild (0).GetComponent<Text> ().text = content;
	}

	public void SetFastVoice(string content){
		SoundPlayer.play (content);
	}

	private void CloseFace(){
		Face.gameObject.SetActive (false);
	}

	private void CloseVoice(){
		voiceimg.gameObject.SetActive (false);
	}

	public void ShowHalo(){
		if (Halo != null) {
			Halo.gameObject.SetActive (true);
			HaloColor = false;
		}
	}

	public void ShowWiner(){
		Winer.gameObject.SetActive (true);
		Winer.transform.DOShakeRotation (1f, 360);
		InvokeRepeating ("HideWiner", 3f, 0f);
	}

	private void HideWiner(){
		Winer.gameObject.SetActive (false);
	}

	public void HideHalo(){
		if (Halo != null) {
			Halo.gameObject.SetActive (false);
		}
	}

	private void CompareCard(GameObject go){
		long[] other = new long[1];
		other [0] = Id;
		GSocket.Send (ByteFactory.makeByte (621115, other));
	}

	public void ClearPlayer(){
		ShowIndex = 0;
		ClearTurn ();
		Id = 0;
		P_name = "";
		P_money = "";
		GameIng = false;
		Head.gameObject.SetActive (false);
		index = 0;
	}

	public void ClearTurn(){
		ShowIndex = 0;
		None = false;
		master = false;
		GameIng = false;
		for(int i = 0;i<cardList.Count;i++)
		{
			GameObject go = cardList[i].gameObject;
			Destroy(go);
		}
		cardList = new List<ZJHCards> ();
		index = 0;
		y = 0;
	}

	public void setHead(string head){
		head = head.Substring (0, head.IndexOf ("/0")+2);
		HeadLoader = new WWW (head);
	}

	public void RenounceCard(){
		BlackCard ();
	}

	public void CompareFiald(){
		InvokeRepeating ("SetFiald", 1f, 0f);
		BlackCard ();
	}

	private void SetFiald(){
		
	}

	private void BlackCard(){
		for( int i = 0 ; i < cardList.Count ; i++ ){
			cardList [i].GetComponent<ZJHCards> ().DoClose ();
		}
		cardListHide ();
	}

	public void setCard(ZJHCards card,float time = 0.5f){

		int x = 0;

		if(CardType == 1){
			if( y == 0 && index > 5 ){
				index = 0;
				y = -50;
			}
		}

		if (Id != info.UserInfo.UserData.id) {
			x = -50 + 60 * index;
		} else {
			x = -50 + 100 * index;
		}

		index++;
		if (CardType == 1) {
			if( cardList.Count > 5 ){
				x -= 20;
			}
		}

		card.transform.SetParent(CardPanel.transform);
		card.transform.DOLocalMove (new Vector3(x, y, 0),time);

		if (Id == info.UserInfo.UserData.id) {

			cardList.Add (card);
			cardListShow (card, list [cardIndex]);
			card.setValue (list [cardIndex]);
			cardIndex++;

			EventTriggerListener.Get (card.gameObject).onClick = SelectCard;
		} else {
			tempList.Add (card);
		}
	}

	public void SelectCard(GameObject go){

		go.GetComponent<ZJHCards> ().setSelect ();
	}

	public void BuildCard(){
		for( int i = 0 ; i < 3 ; i++ ){
			GameObject prefabs = GameObject.Find ("CardsPre");;
			GameObject card =  (GameObject)Instantiate (prefabs,transform);
			setCard (card.GetComponent<ZJHCards>(),0f);
		}
	}

	public void cardListShow(ZJHCards card , int l){
		card.GetComponent<ZJHCards> ().startRotate (l);
	}

	public void cardListHide(){
		for( int i = 0 ; i < cardList.Count ; i++ ){
			cardList [i].GetComponent<ZJHCards> ().startRotate2 ("CardBlack",-30+i*30);
		}
	}


	public void cardListTighten(){
		for( int i = 0 ; i < cardList.Count ; i++ ){
			cardList [i].GetComponent<ZJHCards> ().startRotate3 (-30+i*30);
		}
	}

	public void cardReduction(){
		for (int i = 0; i < cardList.Count; i++) {
			cardList [i].GetComponent<ZJHCards> ().startRotate3 (-50 + i * 100);
		}
	}

	public void CloseSeat(bool close){
		gameObject.SetActive (close);
		Open = close;
	}
	
	// Update is called once per frame
	void Update () {
		if ( !initComplete ) {
			return;
		}

		if (Woner != null && Id != 0) {
			if (info.UserInfo.room.owner == this.Id) {
				Woner.gameObject.SetActive (true);
			} else {
				Woner.gameObject.SetActive (false);
			}
		
		}

		if (Id != 0) {
			Color s = transform.GetChild (0).GetComponent<Image> ().color;
			s.a = 0f;
			transform.GetChild (0).GetComponent<Image> ().color = s;
			if (Halo.gameObject.activeSelf) {
				Color c;
				if (HaloColor) {
					c = Halo.color;
					c.a += 0.02f;
					Halo.color = c;
					if (Halo.color.a >= 1f) {
						HaloColor = false;
					}
				} else {
					c = Halo.color;
					c.a -= 0.02f;
					Halo.color = c;
					if (Halo.color.a <= 0) {
						HaloColor = true;
					}
				}
			}
		} else {
			Color s = transform.GetChild (0).GetComponent<Image> ().color;
			s.a = 1f;
			transform.GetChild (0).GetComponent<Image> ().color = s;
		}


		NameTxt.text = P_name;
		if (P_money != "") {
			MoneyTxt.text =	P_money;
		} else {
			MoneyTxt.text = "";
		}

		ready.gameObject.SetActive(None);
		if ( ZjhView.Igame ) {
			ready.gameObject.SetActive(false);
		}
			
		masterImg.gameObject.SetActive (master);

		if (Id == 0) {
			Head.gameObject.SetActive (false);
		} else {
			Head.gameObject.SetActive (true);
		}


		if (HeadLoader != null) {
			if (HeadLoader.isDone) {
				UserView.setHeadImg (Head, HeadLoader.texture,0,0);
				HeadLoader = null;
			}
		}

		SpreakTime--;
		if (SpreakTime < 0) {
			Spreak.gameObject.SetActive (false);
		} else {
			float height = Spreak.gameObject.transform.GetChild (0).GetComponent<RectTransform> ().sizeDelta.y + 40;

			Spreak.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(Spreak.gameObject.GetComponent<RectTransform>().sizeDelta.x,height);
		}

		offlineImg.gameObject.SetActive(Offline);

		if (Id != info.UserInfo.UserData.id) {
			if (Id == 0) {
				transform.GetChild (0).GetChild (0).gameObject.SetActive (false);
			} else {
				transform.GetChild (0).GetChild (0).gameObject.SetActive (true);
			}
		}

		if(GroupCard.Open){

			if( tempList.Count > 0 ){
				foreach (ZJHCards z in tempList) {
					Destroy (z.gameObject);
					Destroy (z);
				}
				tempList = new List<ZJHCards> ();
			}


		}
	}
}
